//
//  ImageViewWithGesture.m
//  UI04_事件_触摸_手势
//
//  Created by LGC on 16/3/14.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ImageViewWithGesture.h"

@implementation ImageViewWithGesture

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithImage:(UIImage *)image {
    self = [super initWithImage:image];
    if (self) {
        
        // 开启用户交互.
        
        self.userInteractionEnabled = YES;
        
        [self tapGesture];
        [self longPressGesture];
        [self swipGesture];
        [self pinchGesture];
        [self rotationGesture];
        [self panGesture];
    }
    return self;
}

#pragma mark - 轻拍

- (void)tapGesture {
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    
    // 将手势加在对象上.
    [self addGestureRecognizer:tapGesture];
    
    [tapGesture release];
    
    // 设置轻拍的次数.
    tapGesture.numberOfTapsRequired = 2;
    
}

-(void)handleTap: (UITapGestureRecognizer *)tapGesture {
    NSLog(@"轻拍");
    
    // imageView的transform恢复初始状态(动画效果).
    [UIView animateWithDuration:0.5 animations:^{
        
        // imageView的transform恢复初始状态.
        self.transform = CGAffineTransformIdentity;
    }];
}

#pragma mark - 长按 
- (void)longPressGesture {
    
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
    
    [self addGestureRecognizer:longPress];
    
    [longPress release];
    
}

- (void)handleLongPress :(UILongPressGestureRecognizer *)longPress {
    
    // 判断手势开始时.
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"长按");
    }
    
}

#pragma mark - 轻扫
- (void)swipGesture {
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwip:)];
    
    [self addGestureRecognizer:swip];
    
    swip.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [swip release];
    
}

- (void)handleSwip:(UISwipeGestureRecognizer *)swip {
    
    NSLog(@"轻扫");
    
}

#pragma mark - 捏合 
- (void)pinchGesture {
    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
    
    [self addGestureRecognizer:pinch];
    
    [pinch release];
}

- (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
    
    NSLog(@"捏合");
    
    // 修改imageView 对象的transform属性
    self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale);
    
    // 规模为正常
    pinch.scale = 1;
    
}

#pragma mark - 旋转

- (void)rotationGesture {
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotation:)];
    
    [self addGestureRecognizer:rotation];
    
    [rotation release];
    
}

- (void)handleRotation:(UIRotationGestureRecognizer *)rotation {
    
    self.transform = CGAffineTransformRotate(self.transform, rotation.rotation);
    
    
    rotation.rotation = 0;
}

#pragma mark - 位移
- (void)panGesture {
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    
    [self addGestureRecognizer:pan];
    
    [pan release];
    
}
- (void)handlePan:(UIPanGestureRecognizer *)pan {
    
    // 修改imageView本身的transform属性
    self.transform = CGAffineTransformTranslate(self.transform, [pan translationInView:self].x, [pan translationInView:self].y);
    
    // 复位
    [pan setTranslation:CGPointZero inView:self];
    
}
@end
